package cn.lcj.basic.controller;

import cn.lcj.basic.utils.AjaxResult;
import cn.lcj.basic.utils.FastDfsUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * $
 *
 * @return $
 * @date $
 * @description $
 */
@RestController
@RequestMapping("/fastDfs")
public class FastDfsController {
    /**
     * 文件上传接口
     *
     * @param file
     * @return
     */
    //http://localhost:8080/fastDfs/group1/M00/03/8C/CgAIC2PeKiOAGGJNAAAm8GA0XBk948.jpg
    @PostMapping
    public AjaxResult uploadFile(@RequestPart MultipartFile file) {
        try {
            //获取文件名
            String oName = file.getOriginalFilename();
            //获取文件后缀
            String exeName = oName.substring(oName.indexOf(".") + 1);//jpg
            //调用工具类上传文件
            String fileName = FastDfsUtils.upload(file.getBytes(), exeName);
            //上传成功，保存上传路径
            return AjaxResult.me().setObj(fileName);
        } catch (Exception e) {
            e.printStackTrace();
            //上传失败，返回失败信息
            return new AjaxResult(false, "文件上传失败");
        }
    }
    @DeleteMapping
    public  AjaxResult deleteFile(@RequestParam("path") String path){
        try {
           // path=/group1/M00/03/8C/CgAIC2PeKiOAGGJNAAAm8GA0XBk948.jpg
            //获取路径除去前面/
             path = path.substring(1);
            //获取组名
            String groubName = path.substring(0, path.indexOf("/"));
            //获取文件名
            String fileName = path.substring(path.indexOf("/") + 1);
            //工具类删除文件
            FastDfsUtils.delete(groubName,fileName);
            //返回结果
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            //返回删除失败信息
            return new AjaxResult(false,"删除失败");
        }
    }

}